# -*- coding: UTF-8 -*-
# @Time    : 2024/1/14 18:54
# @Author  : Sun
# @Email   : xxxxxxxxxxx@xxx.com
# @File    : find_prop.py
# @software: PyCharm
# 文件注释  :
import time

import xlrd

from common.choice_file import ChoiceFile
from common.read_six_config import ReadXls


class FindProp:
    def __init__(self, xls_prop_name: str = None):
        file_name = ChoiceFile().get_product_name()
        if xls_prop_name is None:
            xls_prop_name = f"../{file_name}"
        self.xls = xlrd.open_workbook(xls_prop_name)
        global user_level
        user_level = 30

    def find_sheet(self, prop_name: str = None):
        """找到对应的道具"""
        reward = None
        # 先找sheet
        if "D" in prop_name:
            reward = self.card_chest_sheet(prop_name)
        elif "P" in prop_name:
            reward = self.pet_xp_sheet(prop_name)
        elif "S" in prop_name:
            reward = self.spin_sheet(prop_name)
        elif "T" in prop_name:
            reward = self.stone_sheet(prop_name)
        elif "B" in prop_name:
            reward = self.tools_sheet(prop_name)
        elif "C" in prop_name:
            reward = self.box_sheet(prop_name)
        elif "F" in prop_name:
            reward = self.pet_food_sheet(prop_name)
        return reward

    def card_chest_sheet(self, prop_name: str = None):
        # print("-----------------------")
        sheet = self.xls.sheet_by_name("card_chest")
        # user_level = 30
        row = 1
        while True:
            # print(row)
            try:
                r = sheet.cell_value(rowx=row, colx=2)  # r是字符串
            except IndexError:
                raise IndexError(f"超时未查找到Card_Chest编号{prop_name}")
            # 判断是否根据岛屿等级区分奖励
            if r == prop_name:
                # print("row+++++++++++", row)
                village_value = sheet.cell_value(rowx=row, colx=4)
                # print("village_value", village_value)
                # 判断是通用奖励还是膨胀奖励
                if village_value == "ALL":
                    if sheet.cell_value(rowx=row, colx=5) != "":
                        card_reward = sheet.cell_value(rowx=row, colx=5)
                        card_reward = {"currency": int(card_reward)}
                        print("card_reward", card_reward)
                        return card_reward
                    elif sheet.cell_value(rowx=row, colx=10) != "":
                        reward = sheet.cell_value(rowx=row, colx=10)
                        card_reward = {"DAFU卡包": reward}
                        print("card_reward", card_reward)
                        return card_reward
                    elif sheet.cell_value(rowx=row, colx=11) != "":
                        # print("row-----", row)
                        reward = sheet.cell_value(rowx=row, colx=11)
                        card_reward = {"coin_master_chest": int(reward)}
                        print("card_reward", card_reward)
                        return card_reward
                    elif sheet.cell_value(rowx=row, colx=12) != "":
                        reward = sheet.cell_value(rowx=row, colx=12)
                        replace_reward = self.card_chest_replace(reward, user_level)
                        card_reward = {"coin_master_card_chest": replace_reward}
                        print("card_reward", card_reward)
                        return card_reward
                else:
                    start = int(time.time())
                    while True:
                        if int(time.time()) - start > 3000:
                            raise TimeoutError(f"超时未查找到card_chest编号{prop_name}")
                        village_value = sheet.cell_value(rowx=row, colx=4)
                        village_value = village_value.strip("[]")
                        # print(village_value)
                        if "," in village_value:
                            village_value_list = village_value.split(",")
                            # print("village_value_list", village_value_list)
                            if int(village_value_list[0]) <= user_level <= int(village_value_list[1]):
                                if sheet.cell_value(rowx=row, colx=5) != "":
                                    reward = sheet.cell_value(rowx=row, colx=5)
                                    card_reward = {"currency": int(reward)}
                                    if sheet.cell_value(rowx=row, colx=11) != "":
                                        reward = sheet.cell_value(rowx=row, colx=11)
                                        card_reward.update({"coin_master_chest": int(reward)})
                                    print("card_reward", card_reward)
                                    return card_reward
                                elif sheet.cell_value(rowx=row, colx=10) != "":
                                    reward = sheet.cell_value(rowx=row, colx=10)
                                    card_reward = {"DAFU卡包": reward}
                                    print("card_reward", card_reward)
                                    return card_reward
                                elif sheet.cell_value(rowx=row, colx=11) != "":
                                    # print("row-----", row)
                                    reward = sheet.cell_value(rowx=row, colx=11)
                                    reward = self.box_list(reward)
                                    card_reward = {"coin_master_chest": reward}
                                    print("card_reward", card_reward)
                                    return card_reward
                                elif sheet.cell_value(rowx=row, colx=12) != "":
                                    reward = sheet.cell_value(rowx=row, colx=12)
                                    # replace_reward = self.card_chest_replace(reward, user_level)
                                    card_reward = {"coin_master_card_chest": int(reward)}
                                    print("card_reward", card_reward)
                                    return card_reward
                                elif sheet.cell_value(rowx=row, colx=13) != "":
                                    tool_reward_name = sheet.cell_value(rowx=row, colx=13)
                                    tool_reward_time = sheet.cell_value(rowx=row, colx=14)
                                    tool_reward_time = tool_reward_time.split(" ")[0]
                                    # 进行拼接，coin_master_rapid_set
                                    tool_reward_name = "coin_master_" + tool_reward_name.replace(" ", "_")
                                    # print(tool_reward_name)
                                    tool_reward = {tool_reward_name: tool_reward_time}
                                    return tool_reward
                            else:
                                row += 1
                                # print("row-------", row)
                                continue
                        elif ">" in village_value:
                            village_value = village_value.strip(">")
                            # print(int(village_value))
                            if user_level > int(village_value):
                                if sheet.cell_value(rowx=row, colx=5) != "":
                                    reward = sheet.cell_value(rowx=row, colx=5)
                                    card_reward = {"currency": int(reward)}
                                    print("card_reward", card_reward)
                                    return card_reward
                                elif sheet.cell_value(rowx=row, colx=10) != "":
                                    reward = sheet.cell_value(rowx=row, colx=10)
                                    card_reward = {"DAFU卡包": reward}
                                    print("card_reward", card_reward)
                                    return card_reward
                                elif sheet.cell_value(rowx=row, colx=11) != "":
                                    # print("row-----", row)
                                    reward = sheet.cell_value(rowx=row, colx=11)
                                    card_reward = {"coin_master_chest": int(reward)}
                                    print("card_reward", card_reward)
                                    return card_reward
                                elif sheet.cell_value(rowx=row, colx=12) != "":
                                    reward = sheet.cell_value(rowx=row, colx=12)
                                    reward = self.box_list(reward)
                                    # replace_reward = self.card_chest_replace(reward, user_level)
                                    card_reward = {"coin_master_card_chest": reward}
                                    print("card_reward", card_reward)
                                    return card_reward
                # print(inspect.currentframe().f_lineno, reward)
            else:
                row += 1
                continue

    def pet_xp_sheet(self, prop_name: str = None):
        setting = self.xls.sheet_by_name("pet_xp")
        row = 1
        start = int(time.time())
        while True:
            if int(time.time()) - start >= 3000:
                raise TimeoutError(f"超时未查找到Pet_Xp编号{prop_name}")
            try:
                # r = setting.row_values(rowx=row, start_colx=2, end_colx=3)  # r是list
                r = setting.cell_value(rowx=row, colx=2)
            except IndexError:
                raise IndexError(f"超时未查找到Pet_Xp编号{prop_name}")
            # print("PetXp", r[0])
            if r == prop_name:
                pet_xp_reward = int(setting.cell_value(rowx=row, colx=8))
                print("pet_xp_bank", pet_xp_reward)
                return {"pet_xp_bank": pet_xp_reward}
            else:
                row += 1
                continue

    def spin_sheet(self, prop_name: str = None):
        setting = self.xls.sheet_by_name("spin")
        row = 1
        start = int(time.time())
        # user_level = 30
        while True:
            if int(time.time()) - start >= 3000:
                raise TimeoutError(f"超时未查找到currency编号{prop_name}")
            try:
                r = setting.cell_value(rowx=row, colx=2)  # r是str
            except IndexError:
                break
            # 找到对应等级就停
            if r == prop_name:
                while True:
                    try:
                        currency_level = setting.cell_value(rowx=row, colx=4)
                        currency_level = currency_level.strip("[]")
                        if "," in currency_level:
                            currency_level = currency_level.split(",")
                            if int(currency_level[0]) <= user_level <= int(currency_level[1]):
                                reward = int(setting.cell_value(rowx=row, colx=5))
                                currency_reward = {"currency": reward}
                                print(currency_reward)
                                return currency_reward
                        elif ">" in currency_level:
                            currency_level = currency_level.strip(">")
                            if user_level > int(currency_level):
                                reward = int(setting.cell_value(rowx=row, colx=5))
                                currency_reward = {"currency": reward}
                                print(currency_reward)
                                return currency_reward
                        row += 1
                    except IndexError:
                        pass
            row += 1

    def stone_sheet(self, prop_name: str = None):
        setting = self.xls.sheet_by_name("stone")
        row = 1
        start = int(time.time())
        while True:
            if int(time.time()) - start >= 3000:
                raise TimeoutError(f"超时未查找到Stone编号{prop_name}")
            try:
                r = setting.cell_value(rowx=row, colx=2)  # r是str
            except IndexError:
                raise IndexError(f"超时未查找到Stone编号{prop_name}")
            # 找到对应等级就停
            if r == prop_name:
                reward = setting.cell_value(rowx=row, colx=6)
                # 这里需要加下stone的算法，得到不同等级应得的stone数
                stone_reward = int(reward)
                stone_reward = {"stones": stone_reward}
                print("stones---------", stone_reward)
                return stone_reward
            else:
                row += 1
                continue

    def tools_sheet(self, prop_name: str = None):
        sheet = self.xls.sheet_by_name("tools")
        # user_level = 30
        row = 1
        start = int(time.time())
        while True:
            if int(time.time()) - start >= 3000:
                raise TimeoutError(f"超时未查找到Tools编号{prop_name}")
            try:
                r = sheet.cell_value(rowx=row, colx=2)  # r是str
            except IndexError:
                raise IndexError(f"超时未查找到Tools编号{prop_name}")
            # 找到对应等级就停
            if r == prop_name:
                village = sheet.cell_value(rowx=row, colx=4)
                if village == "ALL":
                    tool_reward_name = sheet.cell_value(rowx=row, colx=13)
                    tool_reward_time = sheet.cell_value(rowx=row, colx=14)
                    if not isinstance(tool_reward_time, float):
                        tool_reward_time = tool_reward_time.split(" ")[0]
                    # 进行拼接，coin_master_rapid_set
                    tool_reward_name = "coin_master_" + tool_reward_name.replace(" ", "_")
                    # print(tool_reward_name)
                    tool_reward = {tool_reward_name: int(tool_reward_time)}
                    print(tool_reward)
                    return tool_reward
                else:
                    while True:
                        village = sheet.cell_value(rowx=row, colx=4)
                        village = village.strip("[]")
                        # print(village)
                        if "," in village:
                            # print("-----------------------------")
                            village = village.split(",")
                            if int(village[0]) <= user_level <= int(village[1]):
                                tool_reward_name = sheet.cell_value(rowx=row, colx=13)
                                tool_reward_time = sheet.cell_value(rowx=row, colx=14)
                                tool_reward_time = tool_reward_time.split(" ")[0]
                                # 进行拼接，coin_master_rapid_set
                                tool_reward_name = "coin_master_" + tool_reward_name.replace(" ", "_")
                                # print(tool_reward_name)
                                tool_reward = {tool_reward_name: int(tool_reward_time)}
                                print(tool_reward)
                                return tool_reward
                            else:
                                row += 1
                                continue
                        elif ">" in village:
                            village = village.strip(">")
                            if user_level > int(village):
                                tool_reward_name = sheet.cell_value(rowx=row, colx=13)
                                tool_reward_time = sheet.cell_value(rowx=row, colx=14)
                                tool_reward_time = tool_reward_time.split(" ")[0]
                                # 进行拼接，coin_master_rapid_set
                                tool_reward_name = "coin_master_" + tool_reward_name.replace(" ", "_")
                                # print(tool_reward_name)
                                tool_reward = {tool_reward_name: int(tool_reward_time)}
                                print(tool_reward)
                                return tool_reward
                        row += 1
            else:
                row += 1
                # print("row-----------", row)
                continue

    def box_sheet(self, prop_name: str = None):
        setting = self.xls.sheet_by_name("宝箱")
        row = 1
        start = int(time.time())
        while True:
            if int(time.time()) - start >= 3000:
                raise TimeoutError(f"超时未查找到宝箱编号{prop_name}")
            try:
                r = setting.cell_value(rowx=row, colx=2)  # r是str
            except IndexError:
                raise IndexError(f"超时未查找到宝箱编号{prop_name}")
            # 找到对应等级就停
            if r == prop_name:
                reward = setting.cell_value(rowx=row, colx=9)
                reward = reward.split("v")
                print(reward)
                box_reward = {"coin_master_chest": int(reward[1])}
                print("coin_master_chest", box_reward)
                return box_reward
            else:
                row += 1
                continue

    def pet_food_sheet(self, prop_name: str = None):
        setting = self.xls.sheet_by_name("pet_food")
        row = 1
        start = int(time.time())
        while True:
            if int(time.time()) - start >= 3000:
                raise TimeoutError(f"超时未查找到pet_food编号{prop_name}")
            try:
                r = setting.cell_value(rowx=row, colx=2)  # r是str
            except IndexError:
                raise IndexError(f"超时未查找到pet_food编号{prop_name}")
            # 找到对应等级就停
            if r == prop_name:
                reward = setting.cell_value(rowx=row, colx=7)
                pet_food_reward = {"pet_food_bank": int(reward)}
                print("pet_food_bank", pet_food_reward)
                return pet_food_reward
            else:
                row += 1
                continue

    def test(self, product_list: list = None):
        # global user_level
        # user_level = 30
        # 传进来是字符串，先分割
        product_list = ReadXls().get_six_setting()[1]
        print("product_list-------", product_list)
        reward_dict = {}
        lv = 1
        for i in product_list:
            prop_name_list = i.split(",")
            print(f"{lv}等级应给奖励------------", prop_name_list)
            for prop_name in prop_name_list:
                # print(inspect.currentframe().f_lineno, j)
                reward = self.find_sheet(prop_name=prop_name)
                reward_dict.update(reward)
            print("奖励收集结束------", reward_dict)
            print(f"开始LV{lv}奖励校验")
            print(f"结束LV{lv}奖励校验")
            # 校验完数值清空奖励列表
            reward_dict.clear()
            lv += 1

    @staticmethod
    def card_chest_replace(reward, user_level):
        """如果用户等级小于30级，换季卡存在替换逻辑"""
        if isinstance(reward, list):
            reward = reward
        else:
            if user_level < 30:
                if int(reward) == 157:
                    reward = 1
                elif int(reward) == 158:
                    reward = 2
                elif int(reward) == 159:
                    reward = 3
            else:
                reward = int(reward)
        return reward

    @staticmethod
    def box_list(reward):
        """处理宝箱有多个的情况"""
        # 返回"lv1,lv3"，还需进一步处理
        try:
            if "," in reward:
                box_list = []
                reward = reward.split(",")
                for _ in reward:
                    reward = _.split("v")
                    # print(reward[1])
                    box_list.append(int(reward[1]))
                reward = box_list
        except TypeError as e:
            print(e)
            reward = int(reward)
        return reward


if __name__ == '__main__':
    # FindProp().card_chest_sheet(prop_name="D66")
    FindProp().test()
